package com.tqjc.system.core.pipeline.thread;

import com.tqjc.system.common.constant.AddTaskConstant;
import com.tqjc.system.core.pipeline.util.AddTaskUtil;
import com.tqjc.system.core.threadpool.ThreadPoolManager;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 86195
 * @version 1.0
 * @description 任务唤醒线程
 * @date 2023/10/27 16:21
 */
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class TaskWakeThread extends Thread{


    private CarDoingAddTaskThread thread;

    private String carInfo;

    private String workSpanNo;

    @Override
    public void run() {
        while (AddTaskConstant.ACT_AVOID.equals(thread.getLastAct())){
            try {
                if(thread.getState() == State.WAITING){
                    CarProgressLogThread msgThread = CarProgressLogThread.builder().carInfo(carInfo)
                            .msg("该天车睡眠动作被守护唤醒线程唤醒").build();
                    ThreadPoolManager.getInstance().execute(msgThread);
                    synchronized (thread){
                        thread.notify();
                        return;
                    }
                } else {
                    Thread.sleep(1*1000);
                }
            } catch (Exception e){
                log.error("任务id为：{}的任务唤醒线程出现异常",thread.getTaskMaterialBO().getTaskId(),e);
            } finally {
                AddTaskUtil.giveBackThread(workSpanNo);

            }
        }

    }
}
